<?php

function has_saekv() {
    if (defined('SAE_ACCESSKEY') && substr(SAE_ACCESSKEY, 0, 4) == 'kapp')
        return false;
    return in_array('SaeKV', get_declared_classes());
}

if (!has_saekv())
    @mkdir(AROOT . '___kv');

function kget($key) {
    if (has_saekv()) {
        $kv = new SaeKV();
        $kv->init();
        return $kv->get($key);
    } else {
        $keyfile = AROOT . '___kv' . DS . 'kv-' . md5($key);
        return @unserialize(@file_get_contents($keyfile));
    }
}

function kset($key, $value) {
    if (has_saekv()) {
        $kv = new SaeKV();
        $kv->init();
        return $kv->set($key, $value);
    } else {
        $keyfile = AROOT . '___kv' . DS . 'kv-' . md5($key);
        return @file_put_contents($keyfile, serialize($value));
    }
}
